//
//  MyTenderJPVC.swift
//  SlaughterClient
//
//  Created by 邱仙凯 on 2022/3/22.
//  Copyright © 2022 zwb. All rights reserved.
//

import UIKit

class ZyaMyTenderJPVC: ZyaTenderBaseTableVC {
    
    var dataArray =  [ZyaZtbContentModel]()
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.insertIds2Update()
    }
    
    @IBOutlet weak var leftBtnV: UIView!
    @IBOutlet weak var midBtnV: UIView!
    @IBOutlet weak var rightBtnV: UIView!
    @IBOutlet weak var name: UILabel!
     override func viewDidLoad() {
        super.viewDidLoad()
         self.title = "我的"
        name.text = USERINFO.kfName
        tableView.mj_header = KSRefreshHeader {
            self.network()
        }
        tableView.mj_header?.beginRefreshing()
        tableView.register(XSBaojiaCell.self, forCellReuseIdentifier: XSBaojiaCell.identifier)
      
        tableView.tableFooterView = UIView()
        
        
        let tapGesture1 = UITapGestureRecognizer(target: self, action: #selector(leftGo))
        leftBtnV.addGestureRecognizer(tapGesture1)
         let tapGesture2 = UITapGestureRecognizer(target: self, action: #selector(midGo))
         midBtnV.addGestureRecognizer(tapGesture2)
        let tapGesture3 = UITapGestureRecognizer(target: self, action: #selector(rightGo))
        rightBtnV.addGestureRecognizer(tapGesture3)
         if #available(iOS 15.0, *) {
                     tableView.sectionHeaderTopPadding = 0
                 }
         
         
         
       
         
         
 
       NotificationCenter.default.addObserver(self, selector: #selector(newestBillStatusNotification(_:)), name: Notification.Name(newestBillStatus_notification), object: nil)

     
     }
     @objc func newestBillStatusNotification(_ notification: Notification) {
         // 从通知中获取用户信息
         if let userInfo = notification.userInfo,
            let arr = userInfo["newestBillStatus_data"] as? [NewestModel_Z] {
           
             for i in 0..<self.dataArray.count {
                 let ddd = arr.filter{$0.idKey == self.dataArray[i].zZbId}
                 
                 if ddd.count > 0 {
                     self.dataArray[i].zParallel = ddd.first!.zParallel
                     if ddd.first!.zEndDate.unwrap != "" {
                         self.dataArray[i].zEndDate = ddd.first!.zEndDate
                     }
                     if ddd.first!.zOvertimedNum.unwrap != "" {
                         self.dataArray[i].zOvertimedNum = ddd.first!.zOvertimedNum
                     }
                     self.dataArray[i].zBidCount = ddd.first!.zBidCount
                     self.dataArray[i].zPriceRank = ddd.first!.zPriceRank
  
                 }
             }
             self.tableView.reloadData()
      
         }
     }
    deinit {
   
        NotificationCenter.default.removeObserver(self)
    
    }
    @objc func leftGo() {
        
        let view =   ZyaDepositMgtVC()
        self.navigationController?.pushViewController(view, animated: true)
        
    }
    @objc func midGo() {
        let  vc = ZyaJPMyMarkVC()
        
        self.navigationController?.pushViewController(vc, animated: true)
        
    }
    @objc func rightGo() {
        let view = ZyaFeedbackFormVC()
        self.navigationController?.pushViewController(view, animated: true)
        
        
    }
    
    override func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 60
    }
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return self.dataArray.count
        
    }
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        
        let one = self.dataArray[indexPath.row]
   
            guard let cell = tableView.dequeueReusableCell(withIdentifier: XSBaojiaCell.identifier, for: indexPath) as? XSBaojiaCell else {
                fatalError("cell error")
            }
            cell.makeConstrainsts()
            one.innerType = ZTBInnerType.jpMyjpKHW

        one.listType = .JP
            cell.one = one
  
            
            return cell
        
        
        
    }
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        
        var idkey = ""
        
            idkey =  self.dataArray[indexPath.row].zZbId.unwrap
        
        
        var inType:ZTBInnerType!
        
            inType = .chujiaRecordKHW
        
        
        zyago2TenderInfoVC(listType: .JP, fromVC: self, idkey: idkey, innerType: .jpListKHW)
        
    }
    
    override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        
        let headerView = UIView(frame: CGRect(x: 0, y: 0, width: KSGlobal.screenW, height: 30))
        headerView.backgroundColor = .white
        
        let dark = UIView(frame: CGRect(x: 0, y: 0, width: KSGlobal.screenW, height: 10))
        dark.backgroundColor = #colorLiteral(red: 0.9333333333, green: 0.9333333333, blue: 0.9333333333, alpha: 1)
        headerView.addSubview(dark)
        
        let title = UILabel(frame: CGRect(x: 10, y: 18, width: 100, height: 30))
        title.font = UIFont.systemFont(ofSize: 15)
         
            title.text = "我的报价"
        
        
        let btn = UIButton(frame: CGRect(x: KSGlobal.screenW - 100, y: 18, width: 90, height: 30))
        btn.titleLabel?.font = UIFont.systemFont(ofSize: 15)
        btn.tag = section
        btn.setTitleColor(.darkGray, for: .normal)
        btn.addTarget(self, action: #selector(viewAll(sender:)), for: .touchUpInside)
        btn.setTitle("查看全部  >", for: .normal)
        headerView.addSubview(btn)
        headerView.addSubview(title)
        return headerView
    }
    
    @objc func viewAll(sender:UIButton) {
        
        let  vc = ZyaMyTenderJPAllVC()
            self.navigationController?.pushViewController(vc, animated: true)
         
        
        
        
    }
    func network () {
   
            KSNetwork.shared.doRequest(API.tender(.queryMytenderJpJoinList(z_begin_dt: "", z_end_dt: "", z_city: "", z_price_min: "", z_price_max: "", page: "1", size: "3", z_title: "",z_status: "")),showHud: false) { (data:BaseResult<ZyaZtbContentModel>) in
                if data.flag == TRUE {
                    if let infos = data.infos {
                        
                        self.dataArray = infos
                        
                        
                        
                        self.insertIds2Update()

                    }
                    
                } else {
                    
                }
                self.tableView.reloadData()
                self.tableView.mj_header?.endRefreshing()
                
            }
        
        
    }
    
    func insertIds2Update() {
        let ids = self.dataArray.filter{$0.zZbStatus == "2"}.compactMap{$0.zZbId}
        
        GetNewestUtil_Z.shared.appendIds(ids: ids,type: .JP)
 
    }
    
}
